﻿#region

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

#endregion

namespace Radovici.Ctrls.Facebook
{
    [TemplateVisualState(Name = "LoggedIn", GroupName = "LoginStates")]
    [TemplateVisualState(Name = "LoggedOut", GroupName = "LoginStates")]
    public class FacebookConnect : Button
    {
        #region Private fields

        private FacebookApi _api;

        #endregion

        public FacebookConnect()
        {
            DefaultStyleKey = typeof (FacebookConnect);
        }

        #region ApiKey

        // Using a DependencyProperty as the backing store for ApiKey.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ApiKeyProperty = DependencyProperty.Register("ApiKey", 
            typeof (string), typeof (FacebookConnect), new PropertyMetadata(null, OnApiKeyChanged));

        public string ApiKey
        {
            get { return (string) GetValue(ApiKeyProperty); }
            set { SetValue(ApiKeyProperty, value); }
        }

        private static void OnApiKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var connect = d as FacebookConnect;
            if (connect != null)
            {
                if (connect._api != null)
                {
                    connect._api.PropertyChanged -= connect.OnFacebookApiChanged;
                }
                var apiKey = e.NewValue as string;
                if (apiKey != null)
                {
                    connect._api = FacebookApi.GetFacebookApi(apiKey); // new FacebookApi(apiKey);
                    connect._api.PropertyChanged += connect.OnFacebookApiChanged;
                    connect.OnApplyTemplate();
                }
            }
        }

        #endregion

        protected override void OnClick()
        {
            base.OnClick();
            if (_api.IsLoggedIn)
            {
                _api.Logout();
            }
            else
            {
                _api.Login();
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            UpdateStates(false);
        }

        #region Private methods

        private void UpdateStates()
        {
            UpdateStates(true);
        }

        private void UpdateStates(bool useTransitions)
        {
            if (_api.IsLoggedIn)
            {
                VisualStateManager.GoToState(this, "LoggedIn", useTransitions);
            }
            else
            {
                VisualStateManager.GoToState(this, "LoggedOut", useTransitions);
            }
        }

        private void OnFacebookApiChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "IsLoggedIn":
                    UpdateStates();
                    break;
                case "User":
                    break;
            }
        }

        #endregion
    }
}